#include "Listing_20.2_LinkedList.h"
#include <iostream>
using namespace std;

LinkedList::LinkedList(): head(NULL), tail(NULL)
{
  
}

LinkedList::LinkedList(Node *node): head(node), tail(node)
{

}

void LinkedList::addFirst(Node *node)
{
  node->setNext(this->head);
  this->head = node;
  if(this->tail == NULL)
  {
    this->tail = node;
  }
}

Node *LinkedList::getFirst()
{
  return head;
}

void LinkedList::addLast(Node *node)
{  
  this->tail->setNext(node);
  this->tail = node;
  if (this->head == NULL)
  {
    this->head = node;
  }
}

Node *LinkedList::getLast()
{
  return tail;
}

void LinkedList::removeFirst()
{
  Node *current = this->head;
  head = this->head->getNext();
  delete current;
}

void LinkedList::removeLast()
{
  Node *current = this->head;
  while(current->getNext() != this->tail)
  {
    current = current->getNext();
  }

  this->tail = current;
  delete current->getNext();
}
